<?php

namespace app\admin\controller;

use think\Db;
use app\api\model\Privatechat;

class Dynamic extends Common
{
	public function mix()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        $this->assign('name', $search_name);
        $this->assign('search', $search_type);
        if ($search_name && $search_type) {
            if($search_type == 'user_id'){
                $where['t1.' . $search_type] = $search_name;
            }else{
                $where['t1.' . $search_type] = ['like', "%$search_name%"];
            }
        }

        $start = input('start');
        $end = input('end');
        $this->assign('start', $start);
        $this->assign('end', $end);
        if ($start && $end) {
            $where['t1.create_time'] = ['between', [strtotime($start), strtotime($end)]];
        } else if ($start && !$end) {
            $where['t1.create_time'] = ['egt', strtotime($start)];
        } else if (!$start && $end) {
            $where['t1.create_time'] = ['elt', strtotime($end)];
        }

        $state = input('state');
        if ($state || $state === '0') {
            $this->assign('state', $state);
            $where['t1.state'] = $state;
        }

        $data = Db::name('dymanic_collection')
            ->alias('t1')
            ->join('user t2', 't1.user_id = t2.id', 'left')
            ->where($where)
            ->field('t1.*, t2.name, t2.head_portrait')
            ->order('t1.id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function mixShenhe($id, $shenhe, $reason = '')
    {
        $time = time();
        $mix = Db::name('dymanic_collection')->where(['id'=>$id])->find();
        $result = Db::name('dymanic_collection')->where(['id'=>$id])->update(['state' => $shenhe, 'reason' => $reason, 'update_time' => $time]);
        if ($result) {
            switch ((int)$shenhe) {
                case -1:
                    Privatechat::notice($mix['user_id'], "您发布的合集『".$mix['title']."』被驳回" . ($reason ? "，驳回原因：" . $reason : "") . "，请重新编辑后再发布", '');
                    break;
                case 0:
                    Privatechat::notice($mix['user_id'], "您发布的合集『".$mix['title']."』重新审核中", '');
                    break;
                case 1:
                    Privatechat::notice($mix['user_id'], "您发布的合集『".$mix['title']."』审核通过", '');
                    break;
            }
            return ['ret' => '操作成功'];
        } else {
            return ['err' => '操作失败'];
        }
    }

    public function mixshenheAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $shenhe = input('type');
            $reason = '';
            $time = time();
            if (input('?post.delAll')) {
                $post = $post['delAll'];
                $userids = [];
                foreach ($post as $k => $id) {
                    $mix = Db::name('dymanic_collection')->where(['id'=>$id])->find();
                    $userids[] = $mix['user_id'];
                    if ($shenhe == 'del') {
                    } else {
                        switch ((int)$shenhe) {
                            case -1:
                                Privatechat::notice($mix['user_id'], "您发布的合集『".$mix['title']."』被驳回" . ($reason ? "，驳回原因：" . $reason : "") . "，请重新编辑后再发布", '');
                                break;
                            case 0:
                                Privatechat::notice($mix['user_id'], "您发布的合集『".$mix['title']."』重新审核中", '');
                                break;
                            case 1:
                                Privatechat::notice($mix['user_id'], "您发布的合集『".$mix['title']."』审核通过", '');
                                break;
                        }
                    }
                }

                if ($shenhe == 'del') {
                    Db::name('dymanic_collection')->where(['id'=>['in', $post]])->delete();
                	Db::name('dymanic_collection_term')->where(['collection_id' => ['in', $post]])->delete();
                } else {
                    $result = Db::name('dymanic_collection')->where(['id'=>['in', $post]])->update(['state' => $shenhe, 'reason' => $reason, 'update_time' => $time]);
                }
                $userids = array_unique($userids);

                $data = ['ret' => '操作成功'];
            } else {
                $data = ['err' => '操作失败，未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }

    public function mixDele() {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('dymanic_collection')->where(['id' => $id])->delete();
                if ($db) {
                	Db::name('dymanic_collection_term')->where(['collection_id' => $id])->delete();
                    $data = ['ret' => '操作成功'];
                } else {
                    $data = ['err' => '操作失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

	public function report()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        $this->assign('name', $search_name);
        $this->assign('search', $search_type);
        if ($search_name && $search_type) {
            if($search_type == 'user_id'){
                $where['t1.' . $search_type] = $search_name;
            }else{
                $where['t1.' . $search_type] = ['like', "%$search_name%"];
            }
        }

        $start = input('start');
        $end = input('end');
        $this->assign('start', $start);
        $this->assign('end', $end);
        if ($start && $end) {
            $where['t1.create_time'] = ['between', [strtotime($start), strtotime($end)]];
        } else if ($start && !$end) {
            $where['t1.create_time'] = ['egt', strtotime($start)];
        } else if (!$start && $end) {
            $where['t1.create_time'] = ['elt', strtotime($end)];
        }

        $state = input('state');
        if ($state || $state === '0') {
            $this->assign('state', $state);
            $where['t1.state'] = $state;
        }

        $data = Db::name('dymanic_report')
            ->alias('t1')
            ->join('user t2', 't1.user_id = t2.id', 'left')
            ->join('dymanic t3', 't3.dy_id = t1.dy_id', 'left')
            ->where($where)
            ->field('t1.*, t2.name, t2.head_portrait, t3.shenhe, t3.class')
            ->order('t1.id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $page = $data->render();
        $this->assign('data', $data);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function reportShenhe($id, $shenhe, $reason = '')
    {
        $time = time();
        $report = Db::name('dymanic_report')->where(['id'=>$id])->find();
        $result = Db::name('dymanic_report')->where(['id'=>$id])->update(['state' => $shenhe, 'result' => $reason, 'update_time' => $time]);
        if ($result) {
            return ['ret' => '操作成功'];
        } else {
            return ['err' => '操作失败'];
        }
    }

    public function reportshenheAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $shenhe = input('type');
            $reason = '批量设为已处理';
            $time = time();
            if (input('?post.delAll')) {
                $post = $post['delAll'];

                if ($shenhe == 'del') {
                    Db::name('dymanic_report')->where(['id'=>['in', $post]])->delete();
                } else {
                    $result = Db::name('dymanic_report')->where(['id'=>['in', $post]])->update(['state' => $shenhe, 'result' => $reason, 'update_time' => $time]);
                }

                $data = ['ret' => '操作成功'];
            } else {
                $data = ['err' => '操作失败，未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }

    public function reportDele() {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('dymanic_report')->where(['id' => $id])->delete();
                if ($db) {
                    $data = ['ret' => '操作成功'];
                } else {
                    $data = ['err' => '操作失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}